﻿package me.zhengjie.common.config

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import me.zhengjie.common.base.BaseEntity
import me.zhengjie.common.utils.JacksonUtil
import org.apache.commons.lang3.builder.ToStringBuilder
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component



@Component
@ConfigurationProperties("swagger")
data class SwaggerProperties(
    /**
     * 是否开启swagger，生产环境一般关闭，所以这里定义一个变量
     */
    var enable: Boolean? = null,

    /**
     * 项目应用名
     */
    var applicationName: String? = null,

    /**
     * 项目版本信息
     */
    var applicationVersion: String? = null,

    /**
     * 项目描述信息
     */
    var applicationDescription: String? = null,

    /**
     * 接口调试地址
     */
    var tryHost: String? = null
){
    annotation class Update

    override fun toString(): String {
        val builder = ToStringBuilder(this)
        val fields = this.javaClass.declaredFields
        try {
            for (f in fields) {
                f.isAccessible = true
                builder.append(f.name, f[this]).append("\n")
            }
        } catch (e: Exception) {
            builder.append("toString builder encounter an error")
        }
        return builder.toString()
    }

    fun tojson(): String {
        return JacksonUtil.toJSONString(this)!!
    }

    fun copy(source: BaseEntity?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}
